package com.hzhq.contract.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.ObjectError;

import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hzhq
 * @create 2021/3/16 8:39 下午
 * @email hzhq1255@163.com
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    private Integer code;
    private String msg;
    private T data;


    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    public Result(ResultEnum resultEnum, T data) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
        this.data = data;
    }

    public static <T> Result<T> build(Integer code, String msg, T data) {
        return new Result<T>(code, msg, data);
    }

    public static <T> Result<T> build(Integer code, String msg) {
        return new Result<T>(code, msg);
    }

    public static <T> Result<T> build(ResultEnum resultEnum) {
        return new Result<T>(resultEnum);
    }

    public static <T> Result<T> build(ResultEnum resultEnum, T data) {
        return new Result<T>(resultEnum, data);
    }


    public static <T> Result<T> success() {
        return new Result<T>(ResultEnum.SUCCESS);
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(ResultEnum.SUCCESS, data);
    }

    public static <T> Result<T> successWithMsg(String msg) {
        return new Result<T>(ResultEnum.SUCCESS.getCode(), msg, null);
    }

    public static <T> Result<T> successWithData(T data) {
        return new Result<T>(ResultEnum.SUCCESS, data);
    }

    public static <T> Result<T> successWithMsgAndData(String msg, T data) {
        return new Result<T>(ResultEnum.SUCCESS.getCode(), msg, data);
    }


    public static <T> Result<T> error() {
        return new Result<T>(ResultEnum.ERROR);
    }

    public static <T> Result<T> error(T data) {
        return new Result<T>(ResultEnum.ERROR, data);
    }

    public static <T> Result<T> errorWithMsg(String msg) {
        return new Result<T>(ResultEnum.ERROR.getCode(), msg, null);
    }

    public static <T> Result<T> errorWithData(T data) {
        return new Result<T>(ResultEnum.ERROR, data);
    }

    public static <T> Result<T> errorWithMsgAndData(String msg, T data) {
        return new Result<T>(ResultEnum.ERROR.getCode(), msg, data);
    }
}
